/************************************************************************************/
/*  Program name CAPFNDIMP_ORG_LIST.sql                                     */
/*  Creater:  CHAN KA KEUNG BILLY (PCCW - UniHub)                                   */
/*************   History   *************************                                */
/* 09, MAY 2006 CHAN KA KEUNG BILLY     First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER FND_CONCURRENT_PROGRAMS_CT1 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDIMP_ORG_LIST';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_ORG_LIST;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	--  RENAME
	
	FOR C1 IN Cur_F LOOP
					
	
		BEGIN
			INSERT INTO PER_ORGANIZATION_LIST
			VALUES
			(
			CAPBRIMP_MAPPING.GetSecurityProfileMapping(C1.SECURITY_PROFILE_ID)    , 
			CAPBRIMP_MAPPING.GetOrgMapping(C1.ORGANIZATION_ID)        ,
			C1.REQUEST_ID             ,
			C1.PROGRAM_APPLICATION_ID ,
			C1.PROGRAM_ID             ,
			C1.PROGRAM_UPDATE_DATE   
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	/*BEGIN
		FOR C2 IN Cur_G LOOP
			INSERT INTO CFND_CONCURRENT_PROGRAMS VALUES
			(
				C2.Application_ID,
				C2.Concurrent_Program_ID_N,
				CAPBRIMP_MAPPING.GetOrgMapping(C2.S_ORG_ID),
				CAPBRIMP_MAPPING.GetBGMapping(C2.S_BUSINESS_GROUP_ID)
			);
		END LOOP;
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;*/

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER FND_CONCURRENT_PROGRAMS_CT1 ENABLE;
